package com.open.generate.database;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * 数据库类型到Java类型映射
 */
public class DBTypeToBeanMapping {
    private static ResourceBundle bundle = ResourceBundle.getBundle("config/property_mapping");

    public static Map<String, String> stringMap = new HashMap<String, String>();
    public static Map<String, String> IntegerMap = new HashMap<String, String>();
    public static Map<String, String> LongMap = new HashMap<String, String>();
    public static Map<String, String> FloatMap = new HashMap<String, String>();
    public static Map<String, String> DoubleMap = new HashMap<String, String>();
    public static Map<String, String> BooleanMap = new HashMap<String, String>();
    public static Map<String, String> dateMap = new HashMap<String, String>();
    public static Map<String, String> decimalMap = new HashMap<String, String>();

    /**
     * 数据库方言
     */
    public static final String DIALECT;
    /**
     * 查询数据库所有表的信息
     */
    public static final String SQLTABLES;
    /**
     * 查询数据库所有表的数量
     */
    public static final String SQLCOUNT;
    /**
     * 查询表所有列
     */
    public static final String SQLCOLUMNS;
    /**
     * 查询当前表的主键
     */
    public static final String SQL_PRIMARY_KEY;

    static {
        String[] stings = bundle.getString("String").split(",");
        for (String string : stings) {
            stringMap.put(string, "String");
        }
        String[] integers = bundle.getString("Integer").split(",");
        for (String string : integers) {
            IntegerMap.put(string, "Integer");
        }
        String[] longs = bundle.getString("Long").split(",");
        for (String string : longs) {
            LongMap.put(string, "Long");
        }
        String[] floats = bundle.getString("Float").split(",");
        for (String string : floats) {
            FloatMap.put(string, "Float");
        }
        String[] doubles = bundle.getString("Double").split(",");
        for (String string : doubles) {
            DoubleMap.put(string, "Double");
        }
        String[] booleans = bundle.getString("Boolean").split(",");
        for (String string : booleans) {
            BooleanMap.put(string, "Boolean");
        }
        String[] dates = bundle.getString("java.util.Date").split(",");
        for (String string : dates) {
            dateMap.put(string, "java.util.Date");
        }
        String[] decimals = bundle.getString("java.math.BigDecimal").split(",");
        for (String string : decimals) {
            decimalMap.put(string, "java.math.BigDecimal");
        }
        DIALECT = bundle.getString("db.dialect");
        SQLTABLES = bundle.getString(DIALECT + "." + "SQLTables");
        SQLCOLUMNS = bundle.getString(DIALECT + "." + "SQLColumns");
        SQL_PRIMARY_KEY = bundle.getString(DIALECT + "." + "SQL_PRIMARY_KEY");
        SQLCOUNT = bundle.getString(DIALECT + "." + "SQLCount");
    }

    /**
     * 数据库类型转化成Java数据类型
     *
     * @param type 数据库数据类型
     * @return Java对应的数据类型
     * String
     * @throws
     */
    public static String convertType(String type) {
        type = type.toLowerCase();
        if (stringMap.containsKey(type)) {
            return "String";
        } else if (IntegerMap.containsKey(type)) {
            return "Integer";
        } else if (LongMap.containsKey(type)) {
            return "Long";
        } else if (FloatMap.containsKey(type)) {
            return "Float";
        } else if (DoubleMap.containsKey(type)) {
            return "Double";
        } else if (BooleanMap.containsKey(type)) {
            return "Boolean";
        } else if (dateMap.containsKey(type)) {
            return "java.util.Date";
        } else if (decimalMap.containsKey(type)) {
            return "java.math.BigDecimal";
        }
        return type;
    }

    /**
     * 将数据库列名转化成Bean属性名
     *
     * @param column 数据库列名
     * @return Bean属性名
     * String
     * @throws
     */
    public static String columnProperty(String column) {
        String[] split = null;
        if ("Oracle".equalsIgnoreCase(DIALECT)) {
            split = column.toLowerCase().split("_");
        } else {
            split = column.split("_");
        }
        if (split.length > 1) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < split.length; i++) {
                String tempTableName = "";
                if (i == 0) {
                    tempTableName = split[i].substring(0, 1).toLowerCase() + split[i].substring(1, split[i].length());
                } else {
                    tempTableName = split[i].substring(0, 1).toUpperCase() + split[i].substring(1, split[i].length());
                }
                sb.append(tempTableName);
            }
            return sb.toString();
        } else {
            return split[0].substring(0, 1).toLowerCase() + split[0].substring(1, split[0].length());
        }
    }

}